/**
 * 
 */
package com.jonosoft.photocast.viewer.web.client.ui.imagecollections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.jonosoft.photocast.viewer.web.client.ImageCollection;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuItem.CommandDelegate;
import com.jonosoft.photocast.viewer.web.client.util.Size;

/**
 * TODO Add description PhotocastFileList (com.jonosoft.photocast.viewer.web.client.PhotocastFileList)
 *
 * @author jon
 */
public class ImageCollectionsMenuBar extends Composite {

	public static final String STYLE_NAME = "image-collections-menu-bar";
	public static final int WIDTH = 128;

	private final VerticalPanel menubar = new VerticalPanel(); // Vertical
	private final HashMap imageCollectionToMenuItemMap = new HashMap();
	private final ArrayList delegates = new ArrayList();
	private final ClickListener clickListener = new MenuItemClickListener();

	public ImageCollectionsMenuBar(ImageCollectionsMenuItem.CommandDelegate commandDelegate) {
		initWidget(menubar);
		
		addCommandDelegate(commandDelegate);

		menubar.setStyleName(STYLE_NAME);
	}

	public void resize(Size size) {
		menubar.setWidth(size.getCSSWidth());
	}

	public void addImageCollection(ImageCollection imageCollection) {
		buildMenuItems(imageCollection);
	}

	public void addCommandDelegate(CommandDelegate commandDelegate) {
		if (!delegates.contains(commandDelegate))
			delegates.add(commandDelegate);
	}

	public void removeCommandDelegate(CommandDelegate commandDelegate) {
		delegates.remove(commandDelegate);
	}

	public void clearCommandDelegates() {
		delegates.clear();
	}

	private class MenuItemClickListener implements ClickListener {
		/**
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget sender) {
			ImageCollectionsMenuItem menuItem = (ImageCollectionsMenuItem) sender;
			Iterator i = delegates.iterator();

			while (i.hasNext())
				((CommandDelegate) i.next()).onExecute(menuItem);
		}
	}
	
	private void buildMenuItems(ImageCollection imageCollection) {
		final ImageCollectionsByTitleMenuItem menuItem = new ImageCollectionsByTitleMenuItem(imageCollection);
		
		imageCollectionToMenuItemMap.put(imageCollection, menuItem);

		menuItem.addClickListener(clickListener);

		menubar.add(menuItem);
		
		addSubMenuItemsIfNeeded(menuItem);
	}

	/**
     * TODO Add method description for addSubMenuItems
     */
    private void addSubMenuItemsIfNeeded(ImageCollectionsMenuItem menuItem) {
    	if (menuItem instanceof HasSubMenu) {
    		Iterator i = ((HasSubMenu) menuItem).getSubMenuItems().iterator();
    		
    		while (i.hasNext()) {
    			ImageCollectionsMenuItem w = (ImageCollectionsMenuItem) i.next();
    			menubar.add(w);
    			w.addClickListener(clickListener);
    			addSubMenuItemsIfNeeded(w);
    		}
    	}
    }

}
